package com.example.mybaseapp.ui.scale

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mybaseapp.R


class ScaleActivity : AppCompatActivity() {

    private lateinit var constraintLayout: ConstraintLayout
    private lateinit var txGetVersion :TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_scale)
        initView()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun initView() {
        constraintLayout = findViewById<ConstraintLayout>(R.id.main)
        findViewById<Button>(R.id.set_steady_on).apply {
            setOnClickListener { constraintLayout.keepScreenOn = true }
        }
        findViewById<Button>(R.id.set_steady_off).apply {
            setOnClickListener { constraintLayout.keepScreenOn = false }
        }
        findViewById<Button>(R.id.btn_scale2).apply {
            setOnClickListener { startActivity(Intent(this@ScaleActivity, Scale2Activity::class.java)) }
        }
        findViewById<Button>(R.id.btn_scale).apply {
            setOnClickListener { startActivity(Intent(this@ScaleActivity, ScaleActivity::class.java)) }
        }
        txGetVersion =  findViewById<TextView>(R.id.tx_get_version)
        findViewById<Button>(R.id.btn_get_version).apply {
            setOnClickListener {
                txGetVersion.let { it1->
                      val s =  Build.VERSION.INCREMENTAL // 获取软件版本号
                    s?.let{it2->
                        it1.text = it2
                    }
                    Log.d("TAG", "initView: $s")
                }
            }
        }

    }



    private val INVALID_POINTER_ID: Int = -1
    private var scale = 1f
    private var lastTouchX = 0f
    private var lastTouchY:Float = 0f
    private var lastScale = 1f
    private var activePointerId = INVALID_POINTER_ID

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                activePointerId = event.getPointerId(0)
                lastTouchX = event.x
                lastTouchY = event.y
            }

            MotionEvent.ACTION_MOVE -> {
                val pointerIndex = event.findPointerIndex(activePointerId)

                if (pointerIndex != -1) {
                    val x = event.getX(pointerIndex)
                    val y = event.getY(pointerIndex)

                    val dx = x - lastTouchX
                    val dy: Float = y - lastTouchY

                    scale = lastScale + (dx + dy) * 0.01f

                    // 确保缩放比例在 0.1 到 1.0 之间
                    if (scale < 0.1f) {
                        scale = 0.1f
                    } else if (scale > 1.0f) {
                        scale = 1.0f
                    }

                    Log.d("Fire", "onTouchEvent: $scale")
//                    setScaleX(scale)
//                    setScaleY(scale)

                    lastTouchX = x
                    lastTouchY = y
                }
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                lastScale = scale
                activePointerId = INVALID_POINTER_ID
            }
        }
        return true
    }
}